package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.api.R;
import com.heima.common.redis.CacheService;
import com.heima.feign.article.IArticleClient;
import com.heima.model.behavior.dtos.LikeBehaviorDto;
import com.heima.model.behavior.dtos.ReadBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.enums.BehaviorEnums;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.AppThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 许敦军
 * @version 1.0
 * @project heima-leadnews
 * @date 2024/1/24 22:54:12
 */
@Service
@Slf4j
public class UnLikeBehaviorServiceImpl implements com.heima.behavior.service.UnLikeBehaviorServiceImpl {
    @Autowired
    private CacheService cacheService;
    @Autowired
    private IArticleClient iArticleClient;

    /**
     * 喜欢或不喜欢行为
     *
     * @param likeBehaviorDto 喜欢行为dto
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult unLikeBehavior(LikeBehaviorDto likeBehaviorDto) {
        // 参数校验
        if (likeBehaviorDto == null || likeBehaviorDto.getArticleId() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 判断用户是否登陆
        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        if (likeBehaviorDto.getType() == 0) {
            log.info("不喜欢的redis表中新增数据");
            cacheService.hPut(BehaviorEnums.UN_LIKE + likeBehaviorDto.getArticleId(), user.getId().toString(), JSON.toJSONString(likeBehaviorDto));

        } else {
            log.info("取消不喜欢的数据");
            cacheService.hDelete(BehaviorEnums.UN_LIKE + likeBehaviorDto.getArticleId(), user.getId().toString());
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 喜欢行为
     *
     * @param likeBehaviorDto 喜欢行为dto
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult LikesBehavior(LikeBehaviorDto likeBehaviorDto) {
        // 参数校验
        if (likeBehaviorDto == null || likeBehaviorDto.getArticleId() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 判断用户是否登陆
        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        // 判断用户的具体操作
        if (likeBehaviorDto.getOperation() == 0) {
            log.info("点赞行为");
            // 0：点赞行为，首先判断redis中是否已经存在了数据
            Object object = cacheService.hGet(BehaviorEnums.LIKE_BEHAVIOR + likeBehaviorDto.getArticleId(), user.getId().toString());
            if (object != null){
                return ResponseResult.errorResult(AppHttpCodeEnum.LIKED_BEHAVIOR);
            }
            //如果等于空，就说明没有，就存储到redis中
            cacheService.hPut(BehaviorEnums.LIKE_BEHAVIOR+likeBehaviorDto.getArticleId(),user.getId().toString(),likeBehaviorDto.getOperation().toString());
            //更新数据库中的总的点赞数量
            iArticleClient.addLike(likeBehaviorDto);
        }else{
            //取消点赞
            log.info("取消点赞");
            cacheService.hDelete(BehaviorEnums.LIKE_BEHAVIOR+likeBehaviorDto.getArticleId(),user.getId().toString());
            iArticleClient.addLike(likeBehaviorDto);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 阅读行为
     *
     * @param readBehaviorDto 读取行为dto
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult readBehavior(ReadBehaviorDto readBehaviorDto) {
        //参数校验
        if (readBehaviorDto == null || readBehaviorDto.getArticleId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //判断是否登陆
        ApUser user = AppThreadLocalUtil.getUser();
        if(user == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //更改数据
        String readBehavior = (String) cacheService.hGet(BehaviorEnums.READ_BEHAVIOR + readBehaviorDto.getArticleId(), user.getId().toString());
        if(StringUtils.isNotEmpty(readBehavior)){
            ReadBehaviorDto readDto = JSON.parseObject(readBehavior, ReadBehaviorDto.class);
            readBehaviorDto.setCount(readBehaviorDto.getCount()+readDto.getCount());
        }
        log.info("===============添加阅读量==============");
        cacheService.hPut(BehaviorEnums.READ_BEHAVIOR + readBehaviorDto.getArticleId(),user.getId().toString(),JSON.toJSONString(readBehaviorDto));
        iArticleClient.readBehavior(readBehaviorDto);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
